home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / gfx / show / jpegAGAsrc21.lha / jpegAGAsrc / jpegAGA / display.c < prev    next >
C/C++ Source or Header  |  1995-02-02  |  11KB  |  496 lines

  1. /* Screen display routines              */
  2. /* written by Günther Röhrich           */
  3. /* this source is Public Domain         */
  4.  
  5. /* this works only with OS 3.0 or higher */
  6.  
  7. /* last change: 1-Feb-1995 */
  8.  
  9. #define INTUI_V36_NAMES_ONLY
  10.  
  11. #include <exec/types.h>
  12. #include <intuition/intuition.h>
  13. #include <intuition/screens.h>
  14. #include <graphics/modeid.h>
  15.  
  16.  
  17. #ifndef __GNUC__
  18. #include <pragmas/intuition_pragmas.h>
  19. #include <pragmas/graphics_pragmas.h>
  20. #include <pragmas/exec_pragmas.h>
  21. #include <clib/intuition_protos.h>
  22. #include <clib/graphics_protos.h>
  23. #include <clib/exec_protos.h>
  24. #else
  25. #include <inline/graphics.h>  
  26. #include <inline/intuition.h> 
  27. #include <inline/exec.h>
  28. #endif
  29.  
  30. #define HAM8 1
  31. #define ERROR_INTUITION 2
  32. #define ERROR_GRAPHICS 3
  33.  
  34. struct IntuitionBase *IntuitionBase = NULL;
  35. struct GfxBase *GfxBase = NULL;
  36. extern struct Library *IconBase;
  37.  
  38. struct WBFlags
  39. {
  40.  int scale;
  41.  short VGAenable;
  42.  short SUPER72enable;
  43.  short SMRenable;
  44.  short GrayEnable;
  45.  short NoSmooth;
  46.  short DCTFast;
  47.  short ScaleFit;
  48. };
  49.  
  50. extern struct WBFlags WBFlags;
  51. extern short DoNotWait;
  52. extern short finish;
  53.  
  54. extern ULONG SMR_DisplayID;
  55. extern ULONG *ScreenColorTable;
  56. extern ULONG ScaleFitDisplayID;
  57. struct Screen *my_tempscreen  = NULL;
  58. struct Screen *my_screen[2] = {NULL, NULL};
  59. struct Window *my_tempwindow =  NULL;
  60. struct Window *my_window[2] = {NULL, NULL};
  61. struct RastPort temprp; /* a copy of the screen's RastPort */
  62. int Drow; 
  63.  
  64. struct TagItem MyScreenTags[] = {
  65.  
  66. { SA_Width,     (ULONG)0 },
  67. { SA_Height,    (ULONG)0 },
  68. { SA_Depth,     (ULONG)0 },
  69. { SA_DisplayID, (ULONG)0 },
  70. { SA_Behind,    (ULONG)FALSE },
  71. { SA_Colors32,  NULL},
  72. { SA_Parent,    NULL },
  73. { SA_Type,      (ULONG)CUSTOMSCREEN },
  74. { SA_Quiet,     (ULONG)TRUE },
  75. { SA_AutoScroll,(ULONG)TRUE },
  76. { SA_Overscan,  (ULONG)OSCAN_STANDARD },
  77. { TAG_DONE,     (ULONG)TRUE }  };
  78.  
  79. struct TagItem MyWindowTags[] = {
  80.  
  81. { WA_Width,        (ULONG)0 },
  82. { WA_Height,       (ULONG)0 },
  83. { WA_CustomScreen, NULL },
  84. { WA_Activate,     (ULONG)TRUE },
  85. { WA_Left,         (ULONG)0 },
  86. { WA_Top,          (ULONG)0 },
  87. { WA_NoCareRefresh,(ULONG)TRUE },
  88. { WA_Borderless,   (ULONG)TRUE },
  89. { WA_Backdrop,     (ULONG)TRUE },
  90. { WA_RMBTrap,      (ULONG)TRUE }, /* disable screen menu drawing */
  91. { WA_IDCMP,        (ULONG)IDCMP_MOUSEBUTTONS | IDCMP_VANILLAKEY },
  92. { WA_BusyPointer,  (ULONG)TRUE }, /* V39 only! */
  93. { TAG_DONE,        (ULONG)TRUE } };
  94.  
  95. void CloseDisplay(void);
  96. void FlipScreen(void);
  97. void FinalWait(void);
  98.  
  99. /* Initialize the display, colors will be set later */
  100. /* if there is a screen open it in the background as my_screen[1] */
  101.  
  102.  
  103. int InitDisplay(int cols, int rows, ULONG Mode, int NumPlanes)
  104. {
  105.   ULONG Depth, DisplayID;
  106.   ULONG NoSUPER72, i=0;
  107.  
  108.   Drow=0;
  109.  
  110.  
  111.   if(!IntuitionBase)
  112.   {
  113.     if(!(IntuitionBase = (struct IntuitionBase *)OpenLibrary((UBYTE *)"intuition.library", 39)))
  114.     {
  115.      printf("Could not open intuition.library V39 or higher.\n"); 
  116.      return 0;
  117.     }
  118.   }
  119.  
  120.   if(!GfxBase)
  121.   {
  122.     if(!(GfxBase = (struct GfxBase *)OpenLibrary((UBYTE *)"graphics.library",39)))
  123.     {
  124.       printf("Could not open graphics.library V39 or higher.\n");
  125.       return 0;
  126.     }
  127.   }
  128.  
  129.   /* If we already have two screens we must close one */
  130.  
  131.   if(my_screen[0] && my_screen[1])
  132.   {
  133.     FinalWait();
  134.     if(finish) return 0;
  135.     FlipScreen();
  136.   }
  137.  
  138.   if(WBFlags.SMRenable)
  139.   {
  140.     DisplayID = SMR_DisplayID;
  141.   }
  142.   else
  143.   {
  144.     /* Calculate a DisplayID */
  145.     /* this should be done better... */
  146.  
  147.     /* check if SUPER72 mode is available */
  148.  
  149.     NoSUPER72 = ModeNotAvailable(SUPER72_MONITOR_ID | SUPERHAMLACE_KEY);
  150.  
  151.     if(WBFlags.VGAenable)
  152.     {
  153.       if(Mode == HAM8)
  154.       {
  155.         if(cols > 780 && !NoSUPER72 && WBFlags.SUPER72enable) DisplayID = SUPER72_MONITOR_ID | SUPERHAMLACE_KEY;
  156.         else
  157.         if(cols > 370 || rows > 260)
  158.           DisplayID = VGAPRODUCTHAM_KEY;
  159.         else
  160.           DisplayID = VGALORESHAMDBL_KEY;
  161.       }
  162.       else
  163.       {
  164.         if(cols > 780 && !NoSUPER72 && WBFlags.SUPER72enable) DisplayID = SUPER72_MONITOR_ID | SUPERLACE_KEY;
  165.         else
  166.         if(cols > 370 || rows >260) 
  167.           DisplayID = VGAPRODUCT_KEY;
  168.         else
  169.           DisplayID = VGALORESDBL_KEY;        
  170.       }
  171.     }
  172.     else
  173.     {  
  174.       if(Mode == HAM8)
  175.       {
  176.         if(cols > 370 || rows > 260)
  177.           DisplayID = HIRESHAMLACE_KEY;
  178.         else
  179.           DisplayID = HAM_KEY;
  180.       }
  181.       else
  182.       {
  183.         if(cols > 370 || rows >260) 
  184.           DisplayID = HIRESLACE_KEY;
  185.         else
  186.           DisplayID = LORES_KEY; 
  187.       }   
  188.     }
  189.  
  190.     if(ScaleFitDisplayID)
  191.     {
  192.       DisplayID = ScaleFitDisplayID;
  193.       /* printf("ScaleFitDisplayID used.\n"); */
  194.     }
  195.   }
  196.  
  197.   if(Mode == HAM8) Depth = 8;
  198.     else Depth = NumPlanes;
  199.  
  200.  MyScreenTags[0].ti_Data = (ULONG)cols;
  201.  MyScreenTags[1].ti_Data = (ULONG)rows;
  202.  MyScreenTags[2].ti_Data = (ULONG)Depth;
  203.  MyScreenTags[3].ti_Data = (ULONG)DisplayID;
  204.  MyScreenTags[5].ti_Data = (ULONG)((void *)ScreenColorTable);
  205.  
  206.  MyWindowTags[0].ti_Data = (ULONG)cols;
  207.  MyWindowTags[1].ti_Data = (ULONG)rows;
  208.  
  209.   while(1)
  210.   {
  211.     if(my_tempscreen == NULL)
  212.     {
  213.       if(my_screen[0] == NULL)
  214.       {
  215.         MyScreenTags[4].ti_Data = (ULONG)FALSE;
  216.         MyScreenTags[6].ti_Tag = TAG_IGNORE;
  217.         my_tempscreen = OpenScreenTagList(NULL, (struct TagItem *)&MyScreenTags);
  218.       }
  219.       else
  220.       {
  221.         MyScreenTags[4].ti_Data = (ULONG)TRUE;
  222.         MyScreenTags[6].ti_Tag = SA_Parent;
  223.         MyScreenTags[6].ti_Data = (ULONG)((void *)my_screen[0]);
  224.         my_tempscreen = OpenScreenTagList(NULL, (struct TagItem *)&MyScreenTags);
  225.       }
  226.     }
  227.  
  228.     if(my_tempscreen == NULL)
  229.     {
  230.       /* do we already have a screen ? */
  231.       if(my_screen[0])
  232.       {
  233.         printf("Could not open background screen. (out of memory)\n");
  234.         if(!DoNotWait) FinalWait();
  235.         FlipScreen();
  236.         if(finish) return 0;
  237.         continue;
  238.       }
  239.       else return 0;
  240.     }
  241.     
  242.     /* open a dummy window to allow autoscroll feature      */
  243.   
  244.     if(my_tempwindow == NULL && my_tempscreen)
  245.     {
  246.       MyWindowTags[2].ti_Data = (ULONG)((void *)my_tempscreen);
  247.       if(my_screen[0] == NULL)
  248.       {
  249.         MyWindowTags[3].ti_Data = (ULONG)TRUE;
  250.         my_tempwindow = OpenWindowTagList(NULL, (struct TagItem *)&MyWindowTags);
  251.       }
  252.       else
  253.       {
  254.         MyWindowTags[3].ti_Data = (ULONG)FALSE;
  255.         my_tempwindow = OpenWindowTagList(NULL, (struct TagItem *)&MyWindowTags);
  256.       }
  257.     }
  258.  
  259.     if(my_tempwindow == NULL)
  260.     {
  261.       if(my_tempscreen)
  262.       {
  263.         CloseScreen(my_tempscreen);
  264.         my_tempscreen = NULL;
  265.       }
  266.  
  267.       /* do we already have a screen ? */
  268.       if(my_screen[0])
  269.       {
  270.         printf("Could not open window on background screen. (out of memory)\n");
  271.         /* wait for a mouse button press and close it */
  272.         if(!DoNotWait) FinalWait();
  273.         FlipScreen();
  274.         if(finish) return 0;
  275.         continue;
  276.       }
  277.       else return 0;
  278.     }
  279.  
  280.     /* initialize temprp for use with WritePixelLine8() */
  281.  
  282.     if(temprp.BitMap) FreeBitMap(temprp.BitMap);
  283.     CopyMem(&my_tempscreen->RastPort, &temprp, sizeof(struct RastPort));
  284.     temprp.Layer = NULL;
  285.     /* V39 function */
  286.     temprp.BitMap = AllocBitMap(cols, 1, my_tempscreen->RastPort.BitMap->Depth, 0, my_tempscreen->RastPort.BitMap);
  287.  
  288.     if(temprp.BitMap == NULL)
  289.     {
  290.       if(my_tempwindow)
  291.       {
  292.         CloseWindow(my_tempwindow);
  293.         my_tempwindow = NULL;
  294.       }
  295.       if(my_tempscreen)
  296.       {
  297.         CloseScreen(my_tempscreen);
  298.         my_tempscreen = NULL;
  299.       }
  300.  
  301.       /* do we already have a screen ? */
  302.       if(my_screen[0])
  303.       {
  304.         printf("Could not open temporary line bitmap (out of memory)\n");
  305.         /* wait for a mouse button press and close it */
  306.         if(!DoNotWait) FinalWait();
  307.         FlipScreen();
  308.         if(finish) return 0;
  309.         continue;
  310.       }
  311.       else return 0;
  312.     }
  313.   break;
  314.   }
  315.   if(my_screen[0]) i=1;
  316.   my_screen[i] = my_tempscreen;
  317.   my_window[i] = my_tempwindow;
  318.   my_tempscreen = NULL;
  319.   my_tempwindow = NULL;  
  320.  
  321.   if(DoNotWait && my_screen[1]) FlipScreen();
  322.  
  323.   return 1; /* success */  
  324. }
  325.  
  326.  
  327.  
  328. /* Close the display */
  329.  
  330. void CloseDisplay(void)
  331. {
  332.   int i=0;
  333.   if(my_screen[1] && !finish)
  334.   {
  335.     FlipScreen();
  336.     FinalWait();
  337.   }
  338.  
  339.   
  340.   if(my_screen[1]) i=1;
  341.  
  342.   if(temprp.BitMap)
  343.   {
  344.     /* V39 function */
  345.     FreeBitMap(temprp.BitMap);
  346.     temprp.BitMap = NULL;
  347.   }
  348.   for(i=0; i < 2; i++)
  349.   {
  350.     if(my_screen[i]) ScreenToBack(my_screen[i]);
  351.     if(my_window[i])
  352.     {
  353.       CloseWindow(my_window[i]);
  354.       my_window[i] = NULL;
  355.     }
  356.     if(my_screen[i])
  357.     {
  358.       CloseScreen(my_screen[i]);
  359.       my_screen[i] = NULL;
  360.     }
  361.   }
  362.   if(IntuitionBase)
  363.   {
  364.    CloseLibrary((struct Library *)IntuitionBase);
  365.    IntuitionBase = NULL;
  366.   }
  367.   if(GfxBase)
  368.   {
  369.     CloseLibrary((struct Library *)GfxBase);
  370.     GfxBase = NULL;
  371.   }
  372.   if(IconBase)
  373.   {
  374.     CloseLibrary(IconBase);
  375.     IconBase=NULL;
  376.   }
  377. }
  378.  
  379.  
  380. /* display a line of chunky pixel graphics... */
  381.  
  382.  
  383. void DisplayRow(char *array, int cols)
  384. {
  385.   int i=0;
  386.   if(my_screen[1]) i=1;
  387.   if(my_screen[i])
  388.     WritePixelLine8(&my_screen[i]->RastPort, 0, Drow++, cols, (UBYTE *)array, &temprp);
  389. }
  390.  
  391.  
  392. /* check for a right mouse button press (or Esc press) */
  393.  
  394. int CheckButton(void)
  395. {
  396.   struct IntuiMessage *msg;
  397.   int Button = 0;
  398.  
  399.   if(my_window[0])
  400.   {
  401.     while(msg = (struct IntuiMessage *)GetMsg(my_window[0]->UserPort))
  402.     {
  403.       if(msg->Class == IDCMP_MOUSEBUTTONS)
  404.         if(msg->Code == MENUDOWN)
  405.           Button = 1; 
  406.       if(msg->Class == IDCMP_VANILLAKEY)
  407.         if(msg->Code == 27)  /* Esc */
  408.         {
  409.           Button = 2;
  410.           finish = 1;
  411.         }
  412.       ReplyMsg((struct Message *)msg);
  413.       if(Button) break;
  414.     }
  415.   }
  416.   if(Button && my_screen[1] && !finish)
  417.   {
  418.     FlipScreen();
  419.     Button = 0;
  420.   }
  421.   return Button;
  422. }
  423.        
  424. /* final wait after the picture is finished */
  425.  
  426. void FinalWait(void)
  427. {
  428.   struct IntuiMessage *msg;
  429.   int Button = 0;
  430.   struct TagItem WinPointerSet[] = { {WA_Pointer, NULL}, {TAG_DONE, NULL} };
  431.  
  432.   /* always wait for my_screen[0] */
  433.  
  434.  
  435.   /* set the normal pointer */
  436.   /* V39 function */
  437.  
  438.   if(!my_window[0]) return; 
  439.  
  440.   /* SetWindowPointer(my_window[0], WA_Pointer, NULL, TAG_DONE); */
  441.   SetWindowPointerA(my_window[0], WinPointerSet);
  442.   
  443.   if(my_window[0])
  444.   {
  445.     while(!Button)
  446.     {
  447.       Wait(1L<<my_window[0]->UserPort->mp_SigBit);
  448.       while(msg = (struct IntuiMessage *)GetMsg(my_window[0]->UserPort))
  449.       {
  450.         if(msg->Class == IDCMP_MOUSEBUTTONS)
  451.           if(msg->Code == MENUDOWN) Button = 1;
  452.         if(msg->Class == IDCMP_VANILLAKEY)
  453.           if(msg->Code == 27) /* Esc */
  454.           {
  455.             Button = 2;
  456.             finish = 1;
  457.           }
  458.         ReplyMsg((struct Message *)msg);
  459.         if(Button) break;
  460.       }
  461.     }
  462.   }
  463. }
  464.  
  465. void FlipScreen(void)
  466. {
  467.  if(my_screen[0] && my_screen[1])
  468.  {
  469.   ScreenDepth(my_screen[1], SDEPTH_TOFRONT | SDEPTH_INFAMILY, NULL);
  470.   if(my_window[1]) ActivateWindow(my_window[1]);
  471.  }
  472.  else if(my_screen[0]) ScreenToBack(my_screen[0]);
  473.  
  474.  
  475.  if(my_window[0])
  476.  {
  477.    CloseWindow(my_window[0]);
  478.    my_window[0] = NULL;
  479.  }
  480.  
  481.  if(my_screen[0])
  482.  { 
  483.    CloseScreen(my_screen[0]);
  484.    my_screen[0] = NULL;
  485.  }
  486.  
  487.  
  488.  if(my_screen[1])
  489.  {
  490.    my_screen[0] = my_screen[1];
  491.    my_screen[1] = NULL;
  492.    my_window[0] = my_window[1];
  493.    my_window[1] = NULL;
  494.  }
  495. }
  496.